package net.javamine.util;

import java.io.File;
import java.net.URL;

import javax.servlet.http.HttpServletRequest;

import net.javamine.util.entityUtils.dao.EntityUtilDao;

import org.apache.log4j.Logger;

/**
 * 路径相关工具类
 * 
 * @author <a href="mailto: java_lht@163.com">Lht</a>
 * @since 2012-5-24 1.create
 */
public class PathUtils {
	
	private static Logger log = Logger.getLogger(PathUtils.class);
	
	/**
	 * 获取web项目中指定文件夹的绝对路径
	 * @param request
	 * @param lastFolderName 最后一级文件夹的名称，根目录为"/"
	 * @return path
	 */
	public static String getWebSystemPath(HttpServletRequest request, String lastFolderName) {
		String path = request.getSession().getServletContext().getRealPath(lastFolderName);
		path = path.replace("\\", "/");
		return path;
	}
	
    /**
     * 取得当前类所在的文件.Class
     * @param clazz
     * @return
     */
    public static <T> File getClassFile(Class<T> clazz){
        URL path = clazz.getResource(clazz.getName().substring(
                clazz.getName().lastIndexOf(".")+1)+".class");
        if(path == null){
            String name = clazz.getName().replaceAll("[.]", "/");
            path = clazz.getResource("/"+name+".class");
        }
        return new File(path.getFile());
    }
    /**
     * 得到当前类的路径.Class
     * @param <T>
     * @param clazz
     * @return
     */
    public static <T> String getClassFilePath(Class<T> clazz){
        try{
            return java.net.URLDecoder.decode(getClassFile(clazz).getAbsolutePath(),"UTF-8");
        }catch (Exception e) {
        	ExceptionUtils.getExceptionMessage(log, e);
            return null;
        }
    }
    
    /**
     * 取得当前类所在的ClassPath目录，比如tomcat下的classes路径
     * @param clazz
     * @return
     */
    public static <T> File getClassPathFile(Class<T> clazz){
        File file = getClassFile(clazz);
        for(int i=0,count = clazz.getName().split("[.]").length; i<count; i++)
            file = file.getParentFile();
        if(file.getName().toUpperCase().endsWith(".JAR!")){
            file = file.getParentFile();
        }
        return file;
    }
    /**
     * 取得当前类所在的ClassPath路径.Class
     * @param clazz
     * @return
     */
    public static <T> String getClassPath(Class<T> clazz){
        try{
            return java.net.URLDecoder.decode(getClassPathFile(clazz).getAbsolutePath(),"UTF-8");
        }catch (Exception e) {
        	ExceptionUtils.getExceptionMessage(log, e);
            return null;
        }
    }
    
    public static void main(String[] args){
        System.out.println(getClassFilePath(EntityUtilDao.class));
        System.out.println(getClassPath(EntityUtilDao.class));
        System.out.println(System.getProperty("user.dir"));
        File directory = new File(System.getProperty("user.dir"));//设定为当前文件夹    
        try{   
            System.out.println(directory.getCanonicalPath());//获取标准的路径    
            System.out.println(directory.getAbsolutePath());//获取绝对路径    
        }catch(Exception e){}   

    }

}
